package com.coder.oj.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coder.oj.common.core.JsonPage;
import com.coder.oj.common.core.R;
import com.coder.oj.common.entity.Problem;
import com.coder.oj.common.mapper.ProblemMapper;
import com.coder.oj.system.mapstruct.ProblemMapstruct;
import com.coder.oj.system.query.ProblemQuery;
import com.coder.oj.system.vo.ProblemVo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProblemService implements Serializable {

    @Autowired
    private ProblemMapper problemMapper;

    @Autowired
    private ProblemMapstruct problemMapstruct;

    @Autowired
    private CommitService commitService;

    /**
     * 分页查询
     * @author Bosen 2023/1/10 11:37
     **/
    public JsonPage<ProblemVo> getProblemPage(ProblemQuery params) {
        LambdaQueryWrapper<Problem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ObjectUtils.isNotEmpty(params.getStatus()), Problem::getStatus, params.getStatus());
        queryWrapper.and(StringUtils.isNotBlank(params.getName())
                || StringUtils.isNotBlank(params.getSource())
                || StringUtils.isNotBlank(params.getContent()), query -> {
            query.or().like(StringUtils.isNotBlank(params.getName()), Problem::getName, params.getName());
            query.or().like(StringUtils.isNotBlank(params.getSource()), Problem::getSource, params.getSource());
            query.or().like(StringUtils.isNotBlank(params.getContent()), Problem::getContent, params.getContent());
        });
        queryWrapper.eq(StringUtils.isNotBlank(params.getDifficulty()), Problem::getDifficulty, params.getDifficulty());
        IPage<Problem> page = problemMapper.selectPage(new Page<>(params.getPage(), params.getPageSize()), queryWrapper);
        List<ProblemVo> collect = page.getRecords().stream().map(entity -> {
            ProblemVo vo = problemMapstruct.toVO(entity);
            vo.setFlag(commitService.getUserCommitStatusByProblem(params.getUserId(), entity.getId()));
            return vo;
        }).collect(Collectors.toList());
        return new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
    }

    /**
     * 题目推荐
     * @author Bosen 2023/1/30 10:18
     **/
    public List<ProblemVo> getRandomProblem(Integer limit) {
        return problemMapper.getRandomProblem(limit).stream().map(problemMapstruct::toVO).collect(Collectors.toList());
    }

    /**
     * 标签墙
     * @author Bosen 2023/1/30 10:29
     **/
    public List<String> getTagWall() {
        return problemMapper.getTagWall();
    }
}
